import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-07-17
 * Time: 18:28
 */
public class JDBC_Test2 {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        Connection connection=dataSource.getConnection();

        String sql="select book.name as book_name,book.author as book_author,student.name as student_name,borrow_info.start_time,borrow_info.end_time \n" +
                "from book,category,student,borrow_info where book.category_id=category.id and book.id=borrow_info.book_id\n" +
                "and borrow_info.student_id=student.id and category.name='计算机';";

        PreparedStatement statement=connection.prepareStatement(sql);

        ResultSet resultSet=statement.executeQuery();

        while(resultSet.next()){
            String book_name=resultSet.getString("book_name");
            String book_author=resultSet.getString("book_author");
            String student_name=resultSet.getString("student_name");
            Timestamp start_time=resultSet.getTimestamp("start_time");
            Timestamp end_time=resultSet.getTimestamp("end_time");

            System.out.println(book_name+" "+book_author+" "+student_name+" "+start_time+" "+end_time);
        }

        statement.close();
        connection.close();
    }
}
